---
title: Comments
description: Learn about how to interact with Comments using WPGraphQL
---

import Note from '../../src/components/Note'
import GraphiQL from '../../src/components/GraphiQL'

Comments are a core part of WordPress, and they are native in the WPGraphQL Schema. WPGraphQL
provides the ability to query and mutate comments.

## Queries

WPGraphQL provides RootQuery fields built-in to query Comments. You can query for lists of comments
or an individual comment. Below are some examples of querying for comments.

### Comments on a Post

One of the most common use cases for querying a comment is to query for comments associated with
a specific post.

This example shows querying for a specific post, and on that Post, get a list of comments and the
comment children (replies).

```graphql
{
  posts(first: 100) {
    nodes {
      id
      title
      comments {
        nodes {
          ...CommentFields
          replies: children {
            nodes {
              ...CommentFields
              replies: children {
                nodes {
                  ...CommentFields
                  replies: children {
                    nodes {
                      ...CommentFields
                      replies: children {
                        nodes {
                          ...CommentFields
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

fragment CommentFields on Comment {
  id
  date
  type
  approved
  content
}
```

<Note title="Query Fragments">
Here's an example query that shows how to query deeply nested data. One limitation with GraphQL
that is worth noting, is that there's no way to do infinite recursion. You have to specify how many
levels deep you want to query.

In this case, we query for the top level comments, and 4 levels of replies.

While this may seem like a flaw of GraphQL, many would argue that it's actually a benefit, in that
you have complete control over what you're querying and what your application will support.

</Note>

### Root Comments on Post

The example above shows querying a Post and it's comments. One option would also be to load the Post
data initially without comments, then once the page has loaded, make a request from the client to
load the comments.

This could be done like so:

```graphql
query GET_COMMENTS_FOR_POST {
  comments(where: { contentId: 1 }) {
    nodes {
      ...CommentFields
      replies: children {
        nodes {
          ...CommentFields
          replies: children {
            ...CommentFields
            replies: children {
              ...CommentFields
              replies: children {
                ...CommentFields
              }
            }
          }
        }
      }
    }
  }
}

fragment CommentFields on Comment {
  id
  date
  type
  approved
  content
}
```

## Mutations

### Create comment

This is an example of a mutation to create a Comment.

<GraphiQL
  query="
  mutation CREATE_COMMENT($input:CreateCommentInput!){
    createComment(input:$input) {
      comment {
        id
        content
        author {
          ... on CommentAuthor {
            name
          }
        }
      }
    }
  }
  "
  variables={{
    input: {
      clientMutationId: 'CreateComment',
      content: 'This is my first comment',
      author: 'Radomir',
    },
  }}
/>

<Note title="ID Inputs">
At the time of writing this, some inputs, like the one above, that ask for an ID expect the Integer
ID (the database ID), while others throughout the Schema expect the Global ID.

We've got an <a href="https://github.com/wp-graphql/wp-graphql/issues/109">open issue</a> we hope to
address soon that will allow all the ID fields to accept _either_ kind of ID and process
appropriately.

</Note>
